package com.zbw.weightlibrary.WeightSub;

import com.zbw.weightlibrary.Connector.BaseControllmplRongDa;
import com.zbw.weightlibrary.Connector.IinfoCallback;
import com.zbw.weightlibrary.Connector.WeightData;
import com.zbw.weightlibrary.Util.ThreadPoolManager;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

import android_serialport_api.SerialPort;

/**
 * 作者 ：Wesley
 * 时间 ：2023-07-08 10:56
 * 这个类是干嘛的？：RongDaSerialPort
 * 容大串口打印机
 */
public class RongDaSerialPort extends BaseControllmplRongDa {

    private final IinfoCallback mCallback;
    private final String mPort; //串口电子称端口port
    private final int mBaudrate; //串口电子称波特率baudrate
    private SerialPort serialPort;//串口连接
    private InputStream inputStream;//输出流
    private OutputStream outputStream;//输入流

    public RongDaSerialPort(IinfoCallback callback, WeightData data) {
        super(callback);
        mCallback = callback;
        mPort = data.getPort();
        mBaudrate = data.getBaudrate();
    }

    @Override
    public void bleConnect() {
        if(mPort==null){
            mCallback.onConnect("请选择连接的串口",false);
        }else{
            Connect(mPort,mBaudrate);
        }
    }


    @Override
    public void bleDisconnect() {
        ReadWeight = false;
        try {
            if(serialPort != null){
                serialPort.close();
                serialPort = null;
            }
            if(inputStream !=null){
                inputStream.close();
                inputStream = null;
            }
            if(outputStream != null){
                outputStream.close();
                inputStream = null;
            }
            mCallback.onDisconnect("断开成功",true);
        }catch (Exception e){
            mCallback.onDisconnect("断开失败",false);
            e.printStackTrace();
        }
    }

    @Override
    public void getWeight() {
        if(inputStream!=null) {
            ReadWeight(inputStream);
        }
    }

    @Override
    public void stopWeight() {
        ReadWeight = false;
    }

    @Override
    public void clearTare() {
        //清皮
        //容大指令为 01 02 43 03 04
        sendCode(outputStream,"02 43 03 0D");
    }

    @Override
    public void tare() {
        //去皮
        //容大指令为 01 02 54 03 04
        sendCode(outputStream,"02 54 03 0D");
    }

    @Override
    public void zero() {
        //置零
        //容大指令为 01 02 5A 03 04
        sendCode(outputStream,"02 5A 03 0D");
    }

    /**
     * 串口连接
     * @param port 端口
     * @param baudrate 波特率
     */
    private void Connect(final String port, final int baudrate){
        ThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                //连接之前，先判断是否已经连接，如果连接了，就先断开
                Disconnect();
                try {
                    serialPort = new SerialPort(new File("/dev/"+ port),baudrate,0);
                    //获取打开的串口中的输入输出流，以便于串口数据的收发
                    inputStream = serialPort.getInputStream();
                    outputStream = serialPort.getOutputStream();
                    mCallback.onConnect("连接成功",true);
                }catch (Exception e){
                    e.printStackTrace();
                    mCallback.onConnect("连接失败",false);
                }
            }
        });
    }

    /**
     * 连接断开
     */
    private void Disconnect(){
        try {
            if(serialPort != null) {
                bleDisconnect();
                Thread.sleep(500);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
